package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.model.domain.BankAccount;
import com.jzo2o.customer.mapper.BankAccountMapper;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.model.dto.response.BankAccountResDTO;
import com.jzo2o.customer.service.IBankAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 银行账户 服务实现类
 * </p>
 *
 * @author jzh
 * @since 2025-08-08
 */
@Service
public class BankAccountServiceImpl extends ServiceImpl<BankAccountMapper, BankAccount> implements IBankAccountService {

    /**
     * 新增或更新银行账号信息
     * @param bankAccountUpsertReqDTO
     */
    @Override
    public void bankAccount(BankAccountUpsertReqDTO bankAccountUpsertReqDTO) {
        // 获取当前银行账号信息
        Long id = bankAccountUpsertReqDTO.getId();
        BankAccount bankAccount = getById(id);
        if (ObjectUtil.isEmpty(bankAccount)){
            // 说明是新增
            BankAccount account = BeanUtil.toBean(bankAccountUpsertReqDTO, BankAccount.class);
            account.setId(id);
            account.setCreateTime(LocalDateTime.now());
            account.setUpdateTime(LocalDateTime.now());
            save(account);
        } else {
            // 说明是更新
            BankAccount account = BeanUtil.toBean(bankAccountUpsertReqDTO, BankAccount.class);
            account.setUpdateTime(LocalDateTime.now());
            updateById(account);
        }
    }

    /**
     * 获取当前用户银行账号
     * @return
     */
    @Override
    public BankAccountResDTO currentUserBankAccount() {
        Long id = UserContext.getCurrentUser().getId();
        BankAccount bankAccount = getById(id);
        if (ObjectUtil.isEmpty(bankAccount)){
            return new BankAccountResDTO();
        }
        BankAccountResDTO bankAccountResDTO = BeanUtil.toBean(bankAccount, BankAccountResDTO.class);

        return bankAccountResDTO;
    }
}
